<?php

namespace app\common\model\xiluhouse;

use think\Exception;
use think\Hook;
use think\Model;
use traits\model\SoftDelete;

class RentalOrder extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'xiluhouse_rental_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'state_text',
        'lessor_state_text',
        'checkinfo',
        'checkinfo_text',
    ];

    const CHECKINFO = [
        '-3'=>'房东取消',
        '-2'=>'租客取消',
        '-1'=>'经纪人取消',
        '0'=>'待房东签约',
        '1'=>'待租客签约',
        '2'=>'待签约',
        '3'=>'待支付',
        '4'=>'已签约',
    ];


    //状态对应文本消息
    public function getCheckinfoTextAttr($value, $data)
    {
        $checkinfo = $this->getAttr('checkinfo');
        return self::CHECKINFO[$checkinfo];
    }

    /**
     * 获取对订单状态
     */
    public function getCheckinfoAttr($value, $data)
    {
        if ($data['order_state'] == '3') {
            //已完成
            $checkinfo = 4;
        }
        else if ($data['order_state'] == '2') {
            //取消
            if($data['state'] == -1){
                $checkinfo = '-2';
            }else if($data['lessor_state'] == -1){
                $checkinfo = '-3';
            }else{
                $checkinfo = '-1';
            }
        }
        else {
            //进行中
            if($data['state'] == 1 && $data['lessor_state'] == 0){
                $checkinfo = '2'; //待双方签约
            }else if($data['state'] == 1 && $data['lessor_state'] == 1){
                $checkinfo = '1'; //待租客签约
            }else if($data['state'] == 2 && $data['lessor_state'] == 0){
                $checkinfo = '0'; //待房东签约
            }else if($data['state'] == 2 && $data['lessor_state'] == 1){
                if($data['pay_status'] == 0){
                    $checkinfo = '3';
                }else{
                    $checkinfo = '4';
                }
            }
        }
        return $checkinfo;
    }

    
    public function getStateList()
    {
        return ['-1' => __('State -1'), '1' => __('State 1'), '2' => __('State 2'), '3' => __('State 3')];
    }

    public function getLessorStateList()
    {
        return ['0' => __('Lessor_state 0'), '1' => __('Lessor_state 1')];
    }


    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getLessorStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['lessor_state']) ? $data['lessor_state'] : '');
        $list = $this->getLessorStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function rental(){
        return $this->hasOne("RentalOrderHouse",'order_id','id')->setEagerlyType(0);
    }

    public function rentalUser(){
        return $this->belongsTo(User::class,'renter_user_id','id',[],'LEFT')->setEagerlyType(0);
    }

    public function lessorUser(){
        return $this->belongsTo(User::class,'lessor_user_id','id',[],'LEFT')->setEagerlyType(0);
    }

    /**
     * 支付回调
     * @param $order_no
     * @param string $transation_id
     * @return array|bool|false|\PDOStatement|string|Model
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function pay_notify($order_no,$transation_id=''){
        $order = self::where('renter_order_trade_no',$order_no)->find();
        if(!$order){
            throw new Exception('订单不存在');
        }
        if($order->pay_status == '1'){
            throw new Exception('订单已支付完毕');
        }
        $order->pay_status = '1';
        $order->renter_pay_time = time();

        if($order->lessor_state == '1'){
            //判断房东如果已签约则签约完成
            $order->order_state = 3;
            $order->order_contract_image = $order['renter_concat_image'];
            $order->finish_time = time();
        }

        $order->save();

        if($order->lessor_state == 1 && $order->pay_status == 1){
            #增加成交量
            \app\common\model\xiluhouse\UserProfile::where('user_id',$order->user_id)->setInc('deal_num',1);
        }
        //佣金计算与分配
        Hook::listen("xiluhouse_order_divide",$order);
        #佣金日志添加
        Hook::listen('xiluhouse_divide',$order);
        return $order;
    }
}
